<?php
namespace Question\Controller;

use Common\Controller\HomebaseController;
use Label\Model\CategoryModel;
use Order\Model\OrderModel;
use Portal\Model\MsgModel;
use Portal\Model\PostsModel;
use Common\Model\CommentsModel;
use Common\Log\TypeLog;
use Wx\Controller\WxPayController;

class QuestionController extends HomebaseController {

    protected $posts_model;
    protected $comment_model;
    protected $page_size;

    function _initialize() {
        parent::_initialize();
        $this->page_size        = 10;
        $this->posts_model      = D("Portal/Posts");
        $this->comment_model    = D("Common/Comments");
    }


    // 问答列表
    public function index()
    {
        $this->_lists(array("p.post_status" => PostsModel::POST_STATUS_NORMAL, 'p.post_type' => PostsModel::POST_TYPE_DISCUSSION_QUESTION));
        $this->display();
    }

    // 问答详情
    public function question_info()
    {
        $this->check_login();
        $id = I('id', 0, 'intval');
        if ($id) {
            $where = array(
                'p.post_type'       => PostsModel::POST_TYPE_DISCUSSION_QUESTION,
                'p.id'              => $id,
            );
            $info = $this->posts_model->alias('p')->where($where)->find();
            if ($info) {
                if ($info['post_status'] == PostsModel::POST_STATUS_NOT_VERIFY) {
                    $this->error('审核中，请耐心等待~');exit;
                }
                if ($info['post_status'] != PostsModel::POST_STATUS_NORMAL) {
                    $this->error('这个问题溜走了。。');
                }

                // 作者信息
                $question_author = M('users')->where(['id' => $info['post_author']])->find();
                $question_author['avatar'] = sp_get_user_avatar_url($question_author['avatar']);
                $this->assign('question_author', $question_author);

                // 增加浏览次数
                $this->posts_model->where(array('id' => $id))->setInc('post_hits');

                // 设置消息已读
                if ($msg_id = I('msg_id', 0, 'intval')) {
                    M('msg')->where(['id' => $msg_id])->save(['has_read' => 1]);
                }

                // 检查是否能查看答案
                $can_read_answer = $this->_check_can_read_answer($info);
                $this->assign('can_read_answer', $can_read_answer);

                // 方便前端展示
                $short_content = preg_replace('/<img(.*?)>/', '【图片】', $info['post_content']);
                $short_content = preg_replace('/<embed(.*?)>/', '【视频】', $short_content);
                $info['short_content'] = strip_tags($short_content);
                $this->assign('info', $info);

                // 回答
                $where = array(
                    'p.post_type'       => PostsModel::POST_TYPE_DISCUSSION_ANSWER,
                    'p.post_status'     => PostsModel::POST_STATUS_NORMAL,
                    'p.question_id'     => $id,
                );

                $answers = $this->posts_model
                    ->alias('p')
                    ->join('__USERS__ u on p.post_author = u.id')
                    ->where($where)
                    ->order('p.post_order desc, id desc')
                    ->limit(0)
                    ->field("p.*, u.user_nicename, u.avatar, u.position, u.signature, u.user_login")
                    ->select();

                // 回答评论
                $answers_list = array();
                foreach ($answers as $k => $v) {
                    $v['comments']         = $this->_get_answer_comment($v['id']);
                    $v['post_date']        = date("m-d H:i", strtotime($v['post_date']));
                    $v['avatar']           = sp_get_user_avatar_url($v['avatar']);
                    $v['post_author_name'] = $v['user_nicename'] ?: $v['user_login'];
                    $v['comment_count']     = count($v['comments']);

                    if (!$can_read_answer) {
                        unset($v['post_content']);
                    }

                    $answers_list[$v['id']] = $v;
                }

                $this->assign('answers_list', $answers_list);

                // 添加查看记录
                TypeLog::AddQuestionLog(get_current_userid(), $info['post_author'], $id);

                // 接入分享sdk
                if (C('WX_SHARE')) {
                    require_once './plugins/wx_jssdk.php';
                    $jssdk       = new \JSSDK(C('WX_OFFICIAL_ACCOUNT.APP_ID'), C('WX_OFFICIAL_ACCOUNT.APP_SECRET'));
                    $signPackage = $jssdk->GetSignPackage();
                    $this->assign('signPackage', $signPackage);
                }

                // 用户是否登录并且有oa_openid
                $get_base_info = false;
                $user = sp_get_current_user();
                if ($user && !$user['oa_openid']) {
                    $get_base_info = true;
                }
                $this->assign('get_base_info', $get_base_info);

                $this->display();
            } else {
                $this->error('这个问题溜走了~');
            }
        } else {
            $this->error('这个问题溜走了。。');
        }
    }

    // 个人中心，我的问答
    public function center_question()
    {
        $this->check_login();
        $list_index = I('list_index', 0, 'intval');
        $this->assign('list_index', $list_index);

        $order = "p.id desc";

        // 各分类的统计个数
        // 我的提问
        $where      = array('p.post_type' => PostsModel::POST_TYPE_DISCUSSION_QUESTION, 'p.post_author' => sp_get_current_userid());
        $count[]    = $this->posts_model->alias('p')->where($where)->count();

        // 我的回答
        $where      = array('post_type' => PostsModel::POST_TYPE_DISCUSSION_ANSWER, 'post_author' => sp_get_current_userid());
        $ids        = $this->posts_model->where($where)->field('question_id')->select();
        if (!empty($ids)) {
            $ids        = _array_column($ids, 'question_id');
            $where      = array('p.id' => array('in', $ids));
            $count[]    = $this->posts_model->alias('p')->where($where)->count();
        } else {
            $count[]    = 0;
        }

        // 查看的问题
        $model      = M('type_log');
        $where      = ['user_id' => sp_get_current_userid(), 'type' => TypeLog::LOG_TYPE_QUESTION];
        $ids        = $model->where($where)->field('item_id')->select();

        if (!empty($ids)) {
            $ids        = _array_column($ids, 'item_id');
            $where      = array('p.id' => array('in', $ids));
            $count[]    = $this->posts_model->alias('p')->where($where)->count();
        } else {
            $count[]    = 0;
        }

        // 我的新消息
        $model      = M('msg');
        $where      = ['to_uid' => sp_get_current_userid(), 'has_read' => 0, 'type' => ['in', [MsgModel::MSG_TYPE_ADD_ANSWER, MsgModel::MSG_TYPE_ADD_COMMENT]]];
        $count[]    = $model->where($where)->count();

        $this->assign('count_list', $count);

        if ($list_index == 0) {
            // 我的提问
            $where  = array('p.post_type' => PostsModel::POST_TYPE_DISCUSSION_QUESTION, 'p.post_author' => sp_get_current_userid());
            $count  = $this->posts_model->alias('p')->where($where)->count();
            $page   = $this->page($count, $this->page_size);
            $lists  = $this->posts_model
                ->alias('p')
                ->where($where)
                ->limit($page->firstRow, $page->listRows)
                ->order($order)
                ->field("p.*, (SELECT count(*) FROM rx_posts WHERE post_type = 4 and question_id = p.id) as answer_count")
                ->select();

            $this->assign('lists', $lists);
            $this->assign('total_page', ceil($count / $this->page_size));
            $this->assign("page", $page->show('default'));
            $this->display();

        } elseif ($list_index == 1) {

            // 我的回答
            $where  = array('post_type' => PostsModel::POST_TYPE_DISCUSSION_ANSWER, 'post_author' => sp_get_current_userid());
            $ids    = $this->posts_model->where($where)->field('question_id')->select();

            if (!empty($ids)) {
                $ids        = _array_column($ids, 'question_id');
                $where      = array('p.id' => array('in', $ids));
                $count      = $this->posts_model->alias('p')->where($where)->count();
                $page       = $this->page($count, $this->page_size);
                $total_page = ceil($count / $this->page_size);
                $lists      = $this->posts_model->alias('p')->field("p.*, (SELECT count(*) FROM rx_posts WHERE post_type = 4 and question_id = p.id) as answer_count")
                    ->where($where)->limit($page->firstRow, $page->listRows)->order($order)->select();
            } else {
                $count      = 0;
                $lists      = [];
                $page       = $this->page($count, $this->page_size);
                $total_page = 0;
            }

            $this->assign('lists', $lists);
            $this->assign('total_page', $total_page);
            $this->assign("page", $page->show('default'));
            $this->display();


        } elseif ($list_index == 2) {

            // 查看的问题
            $model      = M('type_log');
            $where      = ['user_id' => sp_get_current_userid(), 'type' => TypeLog::LOG_TYPE_QUESTION];
            $ids        = $model->where($where)->field('item_id')->select();

            if (!empty($ids)) {
                $ids        = _array_column($ids, 'item_id');
                $where      = array('p.id' => array('in', $ids));
                $count      = $this->posts_model->alias('p')->where($where)->count();
                $page       = $this->page($count, $this->page_size);
                $total_page = ceil($count / $this->page_size);
                $lists      = $this->posts_model->alias('p')->where($where)->limit($page->firstRow, $page->listRows)->order($order)->select();

            } else {
                $count      = 0;
                $lists      = [];
                $page       = $this->page($count, $this->page_size);
                $total_page = 0;
            }

            $this->assign('lists', $lists);
            $this->assign('total_page', $total_page);
            $this->assign("page", $page->show('default'));
            $this->display();

        } elseif ($list_index == 3) {

            // 我的消息
            $model      = M('msg');
            $where      = ['to_uid' => sp_get_current_userid(), 'has_read' => 0, 'type' => ['in', [MsgModel::MSG_TYPE_ADD_ANSWER, MsgModel::MSG_TYPE_ADD_COMMENT]]];
            $count      = $model->where($where)->count();
            $page       = $this->page($count, $this->page_size);
            $lists      = $model->alias('m')->join("__POSTS__ p on p.id = m.item_id")->field('m.content, m.url, p.post_title, m.time, m.id')->where($where)
                ->limit($page->firstRow, $page->listRows)->order('m.id desc')->select();
            foreach ($lists as $k => $v) {
                $v['time'] = date('Y-m-d H:i:s', $v['time']);
                $lists[$k]  = $v;
            }

            $this->assign('lists', $lists);
            $this->assign('total_page', ceil($count / $this->page_size));
            $this->assign("page", $page->show('default'));
            $this->display();
        } else {
            $this->error('好像跑偏了。。');
        }
    }

    // 添加评论
    public function add_comment()
    {
        $this->check_login();
        $parent_id  = I('post.parent_id', 0, 'intval');
        $answer_id  = I('post.answer_id', 0, 'intval');     // 回答id
        $comment    = I('post.comment');                     // 内容
        $to_uid     = I('to_uid', '0', 'intval');
        $user       = sp_get_current_user();

        $data = array(
            'post_table'        => 'posts',
            'post_id'           => $answer_id,
            'uid'               => $user['id'],
            'to_uid'            => $to_uid,
            'full_name'         => $user['user_nicename'],
            'createtime'        => date("Y-m-d H:i:s"),
            'content'           => $comment,
            'type'              => CommentsModel::COMMENT_TYPE_USER_COMMENT,
            'parentid'         => $parent_id,
            'status'            => 1,
        );
        $id = $this->comment_model->add($data);
        $data['id']= $id;

        // 添加消息
        $answer = $this->posts_model->where(['id' => $answer_id])->find();
        if ($answer['post_author'] != sp_get_current_userid()) {
            $content    = $user['user_nicename'] . '评论了您的回答';
            $url        = "index.php?g=Question&amp;m=Question&amp;a=question_info&amp;id=" . $answer['question_id'];
            MsgModel::add_comment_msg($answer['question_id'], $content, $answer['post_author'], $url);
        }

        $result = array('status' => 1, 'data' => $data);
        $this->ajaxReturn($result);
    }


    // 提问
    public function add_question()
    {
        $this->check_login();
        if (IS_POST) {
            $user       = sp_get_current_user();
            $question   = I("post.post");
            $question['post_author']        = $user['id'];
            $question['post_date']          = date("Y-m-d H:i:s");
            $question['post_status']        = PostsModel::POST_STATUS_NOT_VERIFY;
            $question['post_author_name']  = $user['user_nicename'] ?: $user['user_login'];
            $question['post_type']          = PostsModel::POST_TYPE_DISCUSSION_QUESTION;
            if ($question['post_content']) {
                $question['post_content']   = safe_html(htmlspecialchars_decode($question['post_content']));
            }

            $this->posts_model->add($question);

            $this->success('您的问题已提交审核，请耐心等待');

        } else {
            $this->display();
        }
    }

    // 写回答
    public function add_answer()
    {
        $this->check_login();
        $question_id = I('question_id', 0, 'intval');
        $this->assign('question_id', $question_id);
        if (IS_POST) {

            $user = sp_get_current_user();
            $asnwer['post_content']         = safe_html(htmlspecialchars_decode($_POST['post_content']));
            $asnwer['post_author']          = $user['id'];
            $asnwer['post_date']            = date("Y-m-d H:i:s");
            $asnwer['post_status']          = PostsModel::POST_STATUS_NOT_VERIFY;
            $asnwer['post_author_name']     = $user['user_nicename'];
            $asnwer['post_type']            = PostsModel::POST_TYPE_DISCUSSION_ANSWER;
            $asnwer['question_id']          = $question_id;

            $this->posts_model->add($asnwer);

            // 添加消息
            $question = $this->posts_model->where(['id' => $question_id])->find();
            if ($question['post_author'] != sp_get_current_userid()) {
                $content    = $user['user_nicename'] . '回答了您的问题';
                $url        = "index.php?g=Question&amp;m=Question&amp;a=question_info&amp;id=" . $question_id;
                MsgModel::add_answer_msg($question_id, $content, $question['post_author'], $url);
            }

            $this->success('您的回答已提交审核，请耐心等待');

        } else {
            if ($question_id) {
                $where = array(
                    'p.post_type' => PostsModel::POST_TYPE_DISCUSSION_QUESTION,
                    'p.post_status' => PostsModel::POST_STATUS_NORMAL,
                    'p.id' => $question_id,
                );
                $info = $this->posts_model->alias('p')->where($where)->find();
                if ($info) {

                    // 方便前端展示
                    $short_content = preg_replace('/<img(.*?)>/', '【图片】', $info['post_content']);
                    $short_content = preg_replace('/<embed(.*?)>/', '【视频】', $short_content);
                    $info['short_content'] = strip_tags($short_content);
                    $this->assign('info', $info);
                    $this->display();
                    exit;
                }
            }
            $this->error('这个问题溜走了。。');
        }
    }

    // 回答点赞
    public function like_answer()
    {
        $this->check_login();
        $answer_id = I('answer_id', 0, 'intval');
        if ($answer_id) {
            $info = $this->posts_model->where(['id' => $answer_id])->find();
            if ($info) {
                $re = TypeLog::answerLikeLog(sp_get_current_userid(), $answer_id);
                if ($re['liked'] == 0) {
                    // 之前没赞过
                    $new_post_like = $info['post_like'] + 1;
                } else {
                    $new_post_like = max(0, $info['post_like'] - 1);
                }

                $this->posts_model->where(['id' => $answer_id])->save(['post_like' => $new_post_like]);
                $return = array('status' => 1, 'data' => ['like_count' => $new_post_like]);
                $this->ajaxReturn($return);
            }
        }
    }

    // 回答点赞
    public function dislike_answer()
    {
        $this->check_login();
        $answer_id = I('answer_id', 0, 'intval');
        if ($answer_id) {
            $info = $this->posts_model->where(['id' => $answer_id])->find();
            if ($info) {

                $re = TypeLog::answerDislikeLog(sp_get_current_userid(), $answer_id);
                if ($re['disliked'] == 0) {
                    // 之前没反对过
                    $new_dislike = $info['dislike'] + 1;
                } else {
                    $new_dislike = max(0, $info['dislike'] - 1);
                }

                $this->posts_model->where(['id' => $answer_id])->save(['dislike' => $new_dislike]);
                $return = array('status' => 1, 'data' => ['dislike_count' => $new_dislike]);
                $this->ajaxReturn($return);
            }
        }
    }


    // 支付 -- 查看问题答案
    public function pay_for_question()
    {
        $user_info = sp_get_current_user();
        if (!$user_info) {
            $this->error('请先登录', U('User/Login/index', ['redirect' => base64_encode($_SERVER['REQUEST_URI'])]));exit;
        }
        $question_id    = I('question_id', 0, 'intval');
        $pay_type       = I('get.pay_type');
        $question_info  = $this->posts_model->where(['id' => $question_id, 'post_type' => PostsModel::POST_TYPE_DISCUSSION_QUESTION, 'post_status' => PostsModel::POST_STATUS_NORMAL])->find();
        if ($question_id && $question_info) {

            $user           = sp_get_current_user();
            $order_model    = D('Order/Order');
            $pay_type       = $pay_type ?: OrderModel::ORDER_PAY_TYPE_WX_NATIVE;

            // 检查是否支付过了
            $can_buy = $order_model->checkTicketLimit($user['id'], OrderModel::ORDER_TYPE_QUESTION, $question_id, 1);
            if ($can_buy) {

                // 检查是否有重复订单
                $notBuy = $order_model->CheckOldOrder($user['id'], OrderModel::ORDER_TYPE_QUESTION, $question_id, $question_info['question_price'] * 100);
                if (array_key_exists('trade_order_id', $notBuy)) {
                    // 可以沿用之前的订单
                    $order_id           = $notBuy['order_id'];
                    $trade_order_id     = $notBuy['trade_order_id'];
                } else {

                    $new_order  = $order_model->CreateOrder($user['id'], OrderModel::ORDER_TYPE_QUESTION, $question_id, 1, $question_info['question_price'] * 100);
                    if ($new_order['status'] == 1) {
                        $order_id       = $new_order['order_id'];
                        $trade_order_id = $new_order['trade_order_id'];
                    } else {
                        $this->error('操作失败，请稍后再试');
                        exit;
                    }
                }

                $total_fee  = $question_info['question_price'] * 100; // 支付总额
                $attach     = $pay_type;
                if ($order_id) {
                    if ($pay_type == OrderModel::ORDER_PAY_TYPE_WX_NATIVE) {
                        // 微信支付--native 扫码支付
                        $wx = new WxPayController();
                        $wx->send_native_pay_request("付费问答", $attach, $trade_order_id, $total_fee, $question_id);

                    } elseif ($pay_type == OrderModel::ORDER_PAY_TYPE_WX_H5) {
                        // 微信支付--H5 扫码支付
                        $wx         = new WxPayController();
                        $redirect   = 'http://' . C('DOMAIN_NAME') . '/index.php?g=Question&m=Question&a=question_info&id='.$question_id;
                        $pay_url    = $wx->send_h5_pay_request("付费问答", $attach, $trade_order_id, $total_fee, $redirect);
                        $data       = array('status' => 1, 'pay_url' => $pay_url);
                        $this->ajaxReturn($data);

                    } elseif ($pay_type == OrderModel::ORDER_PAY_TYPE_WX_JSAPI) {
                        // 微信支付--JASPI 公众号内调起微信支付
                        $wx     = new WxPayController();
                        $user   = sp_get_current_user();
                        if (empty($user['oa_openid'])) {
                            session('user', null);
                            $this->error('请用微信重新登录', U('User/Login/index', ['redirect' => base64_encode($_SERVER['REQUEST_URI'])]));
                        } else {
                            $parameters  = $wx->getJsapiPayParameters("付费问答", $attach, $trade_order_id, $total_fee, $question_id, $user['oa_openid']);
                            $this->assign('question_id', $question_id);
                            $this->assign('parameters', $parameters);
                            $this->display();exit;
                        }
                    } else {
                        $this->error('请选择正确的支付方式');
                    }
                }
            } else {
                $this->redirect(U('Question/Question/question_info', ['id' => $question_id]));
            }
        } else {
            $this->error('这个问题溜走了。。');
        }
    }


    public function query_question_pay_order()
    {
        $this->check_login();
        $question_id    = I('question_id', 0, 'intval');
        $question_info  = $this->posts_model->where(['id' => $question_id, 'post_type' => PostsModel::POST_TYPE_DISCUSSION_QUESTION, 'post_status' => PostsModel::POST_STATUS_NORMAL])->find();
        if ($question_id && $question_info) {
            $where      = ['user_id' => sp_get_current_userid(), 'type' => OrderModel::ORDER_TYPE_QUESTION, 'goods_id' => $question_id, 'status' => OrderModel::ORDER_STATUS_HAS_PAY];
            $order_info = M('order')->where($where)->find();

            if ($order_info) {
                $this->success('支付成功');
            } else {
                $this->error('支付失败');
            }
        }
    }


    // 检查用户是否可以查看问题答案
    // return  1 | 0
    public function _check_can_read_answer($question_info = array(), $question_id = 0)
    {
        if (empty($question_info)) {
            $question_info = $this->posts_model->where(['id' => $question_id, 'post_type' => PostsModel::POST_TYPE_DISCUSSION_QUESTION, 'post_status' => PostsModel::POST_STATUS_NORMAL])->find();
        }

        $user_id = sp_get_current_userid();
        if ($question_info['post_author'] == $user_id) {
            return 1;
        } elseif ($question_info['question_price'] == 0) {
            return 1;
        } else {

            // 方式1 -- 回答问题
            $where      = array('post_type' => PostsModel::POST_TYPE_DISCUSSION_ANSWER, 'post_status' => PostsModel::POST_STATUS_NORMAL, 'post_author' => $user_id, 'question_id' => $question_info['id']);
            $has_answer = $this->posts_model->where($where)->find();
            if ($has_answer) {
                return 1;
            }

            // 方式2 -- 支付
            $where      = array('user_id' => $user_id, 'type' => OrderModel::ORDER_TYPE_QUESTION, 'goods_id' => $question_info['id'], 'status' => OrderModel::ORDER_STATUS_HAS_PAY);
            $has_pay    = M('order')->where($where)->find();
            if ($has_pay) {
                return 1;
            }
        }

        return 0;
    }

    // 获取回答评论
    private function _get_answer_comment($post_id)
    {
        $where        = array("post_table" => 'posts', "post_id" => $post_id, "status" => 1, 'type' => CommentsModel::COMMENT_TYPE_USER_COMMENT);
        $comments     = $this->comment_model->where($where)->order("createtime ASC")->select();
        $new_comments = array();

        if (!empty($comments)) {
            foreach ($comments as $m) {

                $m['createtime'] = date("m-d H:i", strtotime($m['createtime']));

                if ($m['parentid'] == 0) {
                    $new_comments[$m['id']] = $m;
                    $new_comments[$m['id']]['children'] = $new_comments[$m['id']]['children'] ?: array();
                } else {
                    $path = explode("-", $m['path']);
                    $new_comments[$path[1]]['children'][] = $m;
                }
            }
        }
        return $new_comments;
    }

    private function _lists($where = array()) {

        $cat_id = I('cat_id', 0, 'intval');
        if ($cat_id) {
            $where[] = "FIND_IN_SET('" . $cat_id . "', p.category)";
            $this->assign('cat_id', $cat_id);
        }

        $count  = $this->posts_model->alias("p")->where($where)->count();
        $page   = $this->page($count, $this->page_size);

        // 缓存值
        $cache_key = "";
        if ($where) {
            $cache_key = md5(implode(',', $where));
        }

        $questions = $this->posts_model
            ->alias("p")
            ->join('__USERS__ u on u.id = p.post_author', 'left')
            ->where($where)
            ->limit($page->firstRow , $page->listRows)
            ->order("p.post_order DESC, p.post_date DESC")
            ->field("p.*, u.user_nicename, u.user_login, (select GROUP_CONCAT(a.post_author, '|', ua.avatar) FROM rx_posts a left join rx_users ua ON a.post_author = ua.id WHERE a.question_id = p.id and a.post_status = " . PostsModel::POST_STATUS_NORMAL . " and a.post_type = " . PostsModel::POST_TYPE_DISCUSSION_ANSWER . ") as answers")
            ->cache("question_list_" . $cache_key . '_' . $page->firstRow . '_' . $page->listRows, C('CACHE_EXPIRE'))
            ->select();

        foreach ($questions as $k => $q) {
            // 提问者
            if (empty($q['post_author_name'])) {
                $q['post_author_name'] = $q['user_nicename'] ?: $q['user_login'];
            }

            // 回答者
            $answers    = $q['answers'] ?: [];
            $answers    = array_filter(explode(',', $answers));
            $answer_arr = array();
            foreach ($answers as $k1 => $v1) {
                list($answer_uid, $avatar) = explode('|', $v1);
                if (!array_key_exists($answer_uid, $answer_arr)) {
                    $answer_arr[$answer_uid] = sp_get_user_avatar_url($avatar);
                }
            }
            $q['answers'] = array_slice($answer_arr, 0, 5);

            $questions[$k] = $q;
        }

        $post_categorys = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_QUESTION, 'status' => 1])->order('id asc')->select();
        $this->assign('post_categorys', $post_categorys);

        $this->assign('count', ceil($count / $this->page_size));
        $this->assign("page", $page->show('default'));
        $this->assign("questions",$questions);
    }
}
